package com.sys.plat.web.core;


import org.apache.struts.action.ActionForm;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ActionContext {
	public static ThreadLocal<HttpServletRequest> requestLocal = new ThreadLocal<HttpServletRequest>();

	public static ThreadLocal<HttpServletResponse> responseLocal = new ThreadLocal<HttpServletResponse>();
	
	public static ThreadLocal<ActionForm>  formLocal = new ThreadLocal<ActionForm> ();
	public static ThreadLocal<String> stringLocal=new ThreadLocal<String> ();

	public static HttpServletRequest getRequest() {
		Object obj = requestLocal.get();
		if (obj != null)
			return requestLocal.get();
		else
			return null;
	}
	
	public static HttpServletResponse getResponse() {
		Object obj = responseLocal.get();
		if (obj != null)
			return responseLocal.get();
		else
			return null;
	}
	
	public static ActionForm getForm() {
		Object obj = formLocal.get();
		if (obj != null)
			return (ActionForm) formLocal.get();
		else
			return null;
	}
	
	public static WebApplicationContext  getWebApplicationContext(){
		return WebApplicationContextUtils.getWebApplicationContext(getRequest().getSession().getServletContext());
	}
	
	public static Object  getBean(String beanName){
		return getWebApplicationContext().getBean(beanName);
	}
}
